﻿using DI;
using Core.IPresentation;
using StructureMap;
using StructureMap.Attributes;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;

namespace Front.Code
{
    public class CultureModule : IHttpModule
    {
        const string DEFAULT_CULTURE = "en-GB";

        private ICultureCookieService cultureCookie;

        public void Dispose()
        {           
        }

        public void Init(HttpApplication context)
        {
            cultureCookie = Bootstrapper.Resolve<ICultureCookieService>();
            context.BeginRequest += context_BeginRequest;            
        }

        void context_BeginRequest(object sender, EventArgs e)
        {            
            CultureInfo ci;
            var culture = cultureCookie.Get();
            if (culture != null)
                ci = new CultureInfo(culture);
            else
                ci = new CultureInfo(DEFAULT_CULTURE);

            Thread.CurrentThread.CurrentUICulture = ci;
            Thread.CurrentThread.CurrentCulture = ci;
        }
    }
}